/*
 * @lc app=leetcode.cn id=476 lang=cpp
 *
 * [476] 数字的补数
 */

// @lc code=start
class Solution
{
public:
  int findComplement(int num)
  {
    int ans = 0, mask = 1;
    while (num != 0) {
      if ((num & 1) == 0)
        ans ^= mask;
      mask <<= 1;
      num >>= 1;
    }
    return ans;
  }
};
// @lc code=end
